package negModel;


import negModelExcep.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Motor extends PartePerformance {

	private int maxrpm;
	private int rpm;
		
	public Motor(int vidaUtil,double peso,float aporteVelMax,String detalle,AlgoPesos precio,boolean removible,int performance,int maxrpm){
		this.setMaxVU(vidaUtil);
		this.arreglar();
		this.setPeso(peso);
		this.setAportevmax(aporteVelMax);
		this.setDetalle(detalle);
		this.setPrecio(precio);
		this.setRemovible(removible);
		this.setPerformance(performance);
		
		this.rpm=0;
		this.maxrpm=maxrpm;
	}
	
	public Motor (Element xmlElement) {
		this.setMaxVU(Integer.parseInt(xmlElement.getAttribute("maxVU")));
		this.setVidaUtil(Integer.parseInt(xmlElement.getAttribute("vidaUtil")));
		this.setPeso(Double.parseDouble(xmlElement.getAttribute("peso")));
		this.setAportevmax(Float.parseFloat(xmlElement.getAttribute("aportevmax")));
		this.setDetalle(xmlElement.getAttribute("detalle"));
		this.setPrecio(new AlgoPesos(Double.parseDouble(xmlElement.getAttribute("precio"))));
		this.setRemovible(Boolean.parseBoolean(xmlElement.getAttribute("removible")));
		this.setPerformance(Integer.parseInt(xmlElement.getAttribute("performance")));
		
		this.maxrpm=Integer.parseInt(xmlElement.getAttribute("maxrpm"));
		this.rpm=Integer.parseInt(xmlElement.getAttribute("rpm"));
	}
	
	public Element toXml(Document doc) {
		Element xmlElement = doc.createElement("parte");
		xmlElement.setAttribute("Clase","motor");
		
		xmlElement.setAttribute("maxVU", ""+ this.getMaxVU());
		xmlElement.setAttribute("vidaUtil", ""+this.getVidaUtil());
		xmlElement.setAttribute("peso", ""+this.getPeso());
		xmlElement.setAttribute("aportevmax", ""+ this.aportevmax());
		xmlElement.setAttribute("detalle", this.getDetalle());
		xmlElement.setAttribute("precio", ""+(this.getPrecio()).getMonto());
		xmlElement.setAttribute("removible", ""+this.esRemovible());
		xmlElement.setAttribute("performance", ""+this.getPerformance());
		
		xmlElement.setAttribute("maxrpm", ""+this.maxrpm);
		xmlElement.setAttribute("rpm", ""+this.rpm);
				
		return xmlElement;
	}
		
	public double funcionar() throws eParteNoFunciona {
		this.gastar();
		return this.getRpm();
	}

	public void aumentarRpm(){
		
		this.rpm+=100;
		
		if (this.rpm>this.maxrpm) 
			this.rpm-=100;
				
	}
	
	public void disminuirRpm (){
		
		this.rpm -= 30;
		
		if (this.rpm < 0)
			this.rpm = 0;
			
	}
	
	//SobreCarga
	public void aumentarRpm(double rpm){
		
		this.rpm += 30;
		
		if (this.rpm > rpm)
			this.disminuirRpm();
		
		
	}
	
	public int getRpm (){
			return (this.rpm);
	}
	
	public int getRpmMax (){
		return (this.maxrpm);
	}
	
	public void agregarMejora(Vehiculo v) {
		
		super.agregarMejora(v);
		((Auto)v).setMotor(this);
		
	}
	
	public void ResetearRpm(){
		rpm=0;
	}
		
	
}
